<?php
class Cart extends AppModel{
    public $useTable  = 'cart';
    public $lang_code = 'vn';
    
    public function add_cart($user = array(), $carts = array())
    {
        try {
            $prefix_cart_id = Configure::read('prefix_cart_id');
            $length_cart_id = Configure::read('length_cart_id');
            $sql = "SELECT cart.cart_id
                    FROM cart
                    ORDER BY cart.cart_id DESC
                    LIMIT 1
                    ";
            $rs = $this->query($sql);
            if(!empty($rs))
            {
                $cart_id = $rs[0]['cart']['cart_id'];
                $number_cart_id = substr($cart_id, -$length_cart_id);
                $number_cart_id  = $number_cart_id + 1;
                
            }
            else
            {
                $number_cart_id = 1;
            }
            $span = '';
            for($i = 1; $i <= ($length_cart_id - strlen($number_cart_id)) ; $i++)
            {
                $span .= '0';
            }
            $cart_id = $prefix_cart_id . $span . $number_cart_id;
                
            $sql = "INSERT INTO cart VALUES('$cart_id', '{$user['user_id']}', now(), now(), 0, '{$user['email']}', '{$user['name']}', '{$user['phone']}', '{$user['address']}')";
            $this->query($sql);
            
            foreach($carts as $product_id => $cart)
            {
                $sql = "INSERT INTO cart_detail VALUES('$cart_id', '$product_id', '{$cart['price']}', '{$cart['discount']}', '{$cart['number']}')";
                $this->query($sql);
            }
            $result = array('status_checkout' => 1, 'cart_id' => $cart_id);
            return $result;
        } catch (Exception $e) {
            $result = array('status_checkout' => $e->getMessage());
            return $result;
        }        
        
    }
    
    public function get_info_cart_($cart_id = null)
    {
        $lang_code = $this->lang_code;
        $sql = "SELECT cart.*, cart_status.title
                FROM cart 
                LEFT JOIN cart_status ON cart.`status` = cart_status.`status` AND cart_status.lang_code = '$lang_code'
                WHERE cart.cart_id = '$cart_id'";
        $cart['cart'] = $this->query($sql);
        
        $sql = "SELECT cart_detail.*, product_desc.title, link.link
                FROM cart_detail 
                LEFT JOIN product_desc ON cart_detail.product_id = product_desc.product_id AND product_desc.lang_code = '$lang_code'
                LEFT JOIN link ON cart_detail.product_id = link.post_id AND type = 'product' AND link.lang_code = '$lang_code'
                WHERE cart_detail.cart_id = '$cart_id'";
        $cart['cart_detail'] = $this->query($sql);
        
        return $cart;
    }
    public function get_info_cart($cart_id = null)
    {
        $lang_code = $this->lang_code;
        $cart = array();
        $sql = "SELECT cart.*, cart_status.title
                FROM cart 
                LEFT JOIN cart_status ON cart.`status` = cart_status.`status` AND cart_status.lang_code = '$lang_code'
                WHERE cart.cart_id = '$cart_id' LIMIT 1";
        $data = $this->query($sql);
        if(!empty($data))
        {
            $cart[0]['cart'] = $data[0]['cart'];
            $cart[0]['cart_status'] = $data[0]['cart_status'];
        }
        
        
        $sql = "SELECT cart_detail.*, product_desc.title, link.link
                FROM cart_detail 
                LEFT JOIN product_desc ON cart_detail.product_id = product_desc.product_id AND product_desc.lang_code = '$lang_code'
                LEFT JOIN link ON cart_detail.product_id = link.post_id AND type = 'product' AND link.lang_code = '$lang_code'
                WHERE cart_detail.cart_id = '$cart_id'";
        $data = $this->query($sql);
        if(!empty($data))
        {
            $cart[0]['cart_detail'] = $data;
        }       
        return $cart;
    }
    
    public function get_info_cart_by_email($email = null)
    {
        $lang_code = $this->lang_code;
        $sql = "SELECT cart.*, cart_status.title
                FROM cart 
                LEFT JOIN cart_status ON cart.`status` = cart_status.`status` AND cart_status.lang_code = '$lang_code'
                WHERE cart.email = '$email' ORDER BY date_added DESC";
        $carts = $this->query($sql);
        $list_cart = array();
        foreach($carts as $cart)
        {
            $cart_id = $cart['cart']['cart_id'];
            $sql = "SELECT cart_detail.*, product_desc.title, link.link
                FROM cart_detail 
                LEFT JOIN product_desc ON cart_detail.product_id = product_desc.product_id AND product_desc.lang_code = '$lang_code'
                LEFT JOIN link ON cart_detail.product_id = link.post_id AND type = 'product' AND link.lang_code = '$lang_code'
                WHERE cart_detail.cart_id = '$cart_id'";
            $cart['cart_detail'] = $this->query($sql);
            $list_cart[] = $cart;
            
        }
        return $list_cart;
    }
}